import { _decorator, Component, EventTouch, Node, Tween, tween, UITransform, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

enum CARD_MOVING_STATUS {
    MOVE_NODE = 0x01,
    EXCHANGE_NODE = 0x10,
}

@ccclass('CardMovingUtil')
export class CardMovingUtil extends Component {
    

    
     
    //===============================手牌移动交互 start=========================================
        /** 初始化移动node 要去的位置fly */
        private _isTouchNode: Node = null!;
        private _isTouchNodeParent: Node = null!;
        private _isChangeNode: Node = null!;
        private _isChangeNodeParent: Node = null!;
        private _isFly: CARD_MOVING_STATUS;
        private onTouchStart(touch: EventTouch) {
            if (this._isTouchNode || this._isChangeNode) {
                return;
            }
            if (this._isFly & CARD_MOVING_STATUS.MOVE_NODE || this._isFly & CARD_MOVING_STATUS.EXCHANGE_NODE) {
                return;
            }
            let _touchBeginPos = touch.getUILocation();
            let _cardNodes = this.getHandCardsNodes();
            this._isChangeNodeParent = null;
            for (let i = 0; i < _cardNodes.length; i++) {
                let _child = _cardNodes[i].children[0];
                if (_child && _child.getComponent(UITransform).getBoundingBoxToWorld().contains(_touchBeginPos)) {
                    // _child.position = this.converPosToNodesLocation(_child.parent, new Vec3(_touchBeginPos.x, _touchBeginPos.y));
                    this._isTouchNodeParent = _cardNodes[i];
                    this._isTouchNode = _child;
                    this._isTouchNode.parent = this.lay_moveAni;
                    this._isTouchNode.scale = this._isTouchNodeParent.scale;
                    this._isTouchNode.position = this.converPosToNodesLocation(this.lay_moveAni, new Vec3(_touchBeginPos.x, _touchBeginPos.y));
                    break;
                }
            }
        }
        private onTouchMove(touch: EventTouch) {
            if (this._isFly & CARD_MOVING_STATUS.MOVE_NODE || this._isFly & CARD_MOVING_STATUS.EXCHANGE_NODE || !this._isTouchNode) {
                return;
            }
            let _nowTouchWorldPos = touch.getUILocation();
            // let delayPos = new Vec2(_nowTouchWorldPos.x - this.lastBetPosCache.x, _nowTouchWorldPos.y - this.lastBetPosCache.y);
            if (this._isTouchNode) {
                this._isTouchNode.position = this.converPosToNodesLocation(this.lay_moveAni, new Vec3(_nowTouchWorldPos.x, _nowTouchWorldPos.y));
            }
        }
        private onTouchEnd(touch: EventTouch) {
            this.dealTouchEnd(touch);
        }
        private onTouchCancel(touch: EventTouch) {
            this.dealTouchEnd(touch);
        }
        private dealTouchEnd(touch: EventTouch) {
            if (this._isFly & CARD_MOVING_STATUS.MOVE_NODE || this._isFly & CARD_MOVING_STATUS.EXCHANGE_NODE || !this._isTouchNode) {
                return;
            }
            let _nowTouchWorldPos = touch.getUILocation();
            let _cardNodes = this.getHandCardsNodes();
            this._isChangeNode = this._isTouchNode;
            this._isTouchNode = null;
            this._isChangeNodeParent = null;
            this._isFly |= CARD_MOVING_STATUS.MOVE_NODE;
            for (let i = 0; i < _cardNodes.length; i++) {
                let _changedParent = _cardNodes[i];
                if (_changedParent != this._isChangeNode.parent && _changedParent.getComponent(UITransform).getBoundingBoxToWorld().contains(_nowTouchWorldPos)) {
                    this._isChangeNodeParent = _changedParent;
                    let _child = _changedParent.children[0];
                    if (_child) {
                        // 交换节点  将节点移动至动画层
                        this._isTouchNode = _child;
                        let _worldPos = _child.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
                        this._isTouchNode.parent = this.lay_moveAni;
                        this._isTouchNode.scale = this._isChangeNodeParent.scale;
                        this._isTouchNode.position = this.converPosToNodesLocation(this.lay_moveAni, _worldPos);
                        this._isFly |= CARD_MOVING_STATUS.EXCHANGE_NODE;
                    }
                    break;
                }
            }
            if (!this._isChangeNodeParent) {
                this._isChangeNodeParent = this._isTouchNodeParent;
            }
            if (this._isTouchNode) {
                let _delayPos = this.getWolrdARDistance(this._isTouchNodeParent, this._isTouchNode);
                let _time = Vec3.len(_delayPos) / 1500;
                if (_time < 0.08) {
                    _time = 0.08;
                }
                if (_time > 0.25) {
                    _time = 0.25;
                }
                tween(this._isTouchNode).by(_time, { position: _delayPos }).call(() => {
                    this._isTouchNode.parent = this._isTouchNodeParent;
                    this._isTouchNode.scale = Vec3.ONE;
                    this._isTouchNode.position = Vec3.ZERO;
                    this._isTouchNode = null;
                    this._isTouchNodeParent = null;
                    this.scheduleOnce(() => {
                        this._isFly -= CARD_MOVING_STATUS.EXCHANGE_NODE;
                    })
                }).start();
            }
            if (this._isChangeNode) {
                let _delayPos = this.getWolrdARDistance(this._isChangeNodeParent, this._isChangeNode);
                let _time = Vec3.len(_delayPos) / 1500;
                if (_time < 0.08) {
                    _time = 0.08;
                }
                if (_time > 0.25) {
                    _time = 0.25;
                }
                tween(this._isChangeNode).by(_time, { position: _delayPos }).call(() => {
                    this._isChangeNode.parent = this._isChangeNodeParent;
                    this._isChangeNode.scale = Vec3.ONE;
                    this._isChangeNode.position = Vec3.ZERO;
                    this._isChangeNode = null;
                    this._isChangeNodeParent = null;
                    this.scheduleOnce(() => {
                        this._isFly -= CARD_MOVING_STATUS.MOVE_NODE;
                    })
                }).start();
            }
        }
        /** 断线重连调用方法，然后将牌重新回原位 */
        private clearTouchNode() {
            if (this._isTouchNode) {
                Tween.stopAllByTarget(this._isTouchNode);
                this._isTouchNode.position = Vec3.ZERO;
                this._isTouchNode.parent = this._isTouchNodeParent;
                this._isTouchNode = null;
                this._isTouchNodeParent = null;
            }
            if (this._isChangeNode) {
                Tween.stopAllByTarget(this._isChangeNode);
                this._isChangeNode.position = Vec3.ZERO;
                this._isChangeNode.parent = this._isChangeNodeParent;
                this._isChangeNode = null;
                this._isChangeNodeParent = null;
            }
            this._isFly = 0;
        }
     
     
        /**
         * converPosToNodesLocation 方法
         * 世界左边转换为Node坐标
         * @param _node 
         * @param _pos 
         * @returns 
         */
        public converPosToNodesLocation(_node: Node, _pos: Vec3) {
            return _node.getComponent(UITransform).convertToNodeSpaceAR(_pos);
        }
     
        /**
         * 获取node1到node2
         * @param node1 
         * @param node2 
         * @returns 
         */
        public getWolrdARDistance(node1: Node, node2: Node): Vec3 {
            let pos1 = node1.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
            let pos2 = node2.getComponent(UITransform).convertToWorldSpaceAR(Vec3.ZERO);
            return new Vec3(pos1.x - pos2.x, pos1.y - pos2.y, pos1.z - pos2.z);
        }
    //===============================手牌移动交互 end=========================================
         

}


